<template>
  <div>
    {{value}}
  <el-tree-select
    v-model="value"
    :data="cursiveData.nvsData.nvs"
    check-strictly
    :props="treeProps"
    :render-after-expand="false"
    @change = "change"
    style="width: 240px"
  />
    <el-button type="text" @click="addNode()">增加</el-button>
    <el-button type = 'text' @click="deleteNode()">删除</el-button>
  </div>
</template>

<script>
import { ref, inject, onMounted } from 'vue'
import ConstDef from '../../js/ConstDef'
import counterManage from '../../cache/CounterManage'
import nvsDataUtils from '../../js/NvsDataUtils'
import nvService from '../../api/NvService'
import cacheManage from '../../cache/CacheManage'
export default {
  name: 'TreeInput',
  props: {
    fvData: {
      default: null,
      type: Object
    }
  },
  setup(props) {
    const currentNv = ref();
    const value = ref();
    const cursiveData = inject('cursiveData', null);
    onMounted(() => {
      const localFvData = props.fvData;
      localFvData.fdLayout.functionCode = ConstDef.FUNCTION_CODE_CURSIVE_TREE_CHANGE;
    })
    const treeProps = {
      label: props.fvData.fdLayout.name,
      value: 'id',
      children: props.fvData.nd.ndName
    }
    return {
      value,
      currentNv,
      treeProps,
      cursiveData
    }
  },
  methods: {
    change(value) {
      const localFvData = this.fvData;
      if (localFvData.getNv().id === 0) {
        localFvData.nv.id = counterManage.getNegativeCount();
      }
      this.cursiveData.currentNvId = value;
      this.value = value;
      // this.$emit("define-event", { fvData: this.fvData, value })
    },
    async addNode() {
      if (this.cursiveData.currentNvId === 0) { return; }
      const currentNv = nvsDataUtils.searchNvById(this.cursiveData.currentNvId, this.cursiveData.nvsData.nvs, this.cursiveData.nvsData.nd);
      if (currentNv != null) {
        const commonInfo = cacheManage.getDefaultCommonInfo();
        const nvMsg = await nvService.initNv(commonInfo, this.cursiveData.nvsData.nd.ndCode);
        if (!nvMsg.success) {
          return;
        }
        const ndName = this.cursiveData.nvsData.nd.ndName;
        if (nvMsg.value.id === 0) nvMsg.value.id = counterManage.getNegativeCount();
        this.cursiveData.currentNvId = nvMsg.value.id;
        this.value = nvMsg.value.id;
        currentNv[ndName].push(nvMsg.value);
        // this.$emit("define-event", { fvData: this.fvData, value: this.cursiveData.currentNvId })
      }
    },
    deleteNode() {

    }
  }
}
</script>

<style scoped>

</style>
